<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addpermission(1, '0')">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button v-if="row.type === 1" size="small" type="text" @click="addpermission(2, row.id)">添加权限点</el-button>
              <el-button size="small" type="text" @click="openEditDialog(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="delPermission(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 新增弹框 -->
    <add-permission ref="addpermissionDialog" @updata="getPermissionList" />
  </div>
</template>

<script>
import { getPermissionListApi, delPermissionApi } from '@/api/permission'
import { tileListToTreeData } from '@/utils/index'
import AddPermission from './components/add-permission.vue'
export default {
  name: 'Permission',
  components: {
    AddPermission
  },
  data() {
    return {
      list: [],
      pid: ''
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 打开编辑信息
    openEditDialog(id) {
      this.$refs.addpermissionDialog.getPermissionInfo(id)
    },
    // 获取权限列表
    async getPermissionList() {
      const { data: res } = await getPermissionListApi()
      this.list = tileListToTreeData(res, '0')
    },
    // 删除
    async delPermission(id) {
      try {
        await this.$confirm('确定要删除吗', '提示')
      } catch (error) {
        return console.log(error)
      }
      await delPermissionApi(id)
      this.$message.success('删除成功')
      this.getPermissionList()
    },
    // 新增
    addpermission(type, pid) {
      // console.log(type, pid)
      this.$refs.addpermissionDialog.openDialog(type, pid)
    }
  }
}
</script>

<style>

</style>
